package com.util;

public class SortUtil {
    static void bubbleSort(int[] a){
        int length = a.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = i+1; j < length; j++) {
                if (a[i] > a[j]){
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    static void optimizeBubbleSort(int[] a){
        int n = a.length;
        boolean swapMark = false;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                    swapMark = true;
                }
            }
            if (!swapMark){
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {2,3,5,0,7};
        optimizeBubbleSort(a);
        for (int i : a) {
            System.out.print(i);
        }
    }
}
